package org.example.user.flight;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

//展示当前所有航线的信息,然后在选择航线起点和终点,再选定航班进行订单添加(注意是订单的添加,不是航班的添加)
public class ShowUserFlightInfoView extends JFrame {
    private JTable table;
    private JButton buttonAdd, buttonSearch;

    public ShowUserFlightInfoView() {
        setTitle("用户端航班查看页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());


        // 创建表格模型并设置表格数据
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("航线起点");
        model.addColumn("航线终点");
        model.addColumn("航班id");
        model.addColumn("航班价格");
        model.addColumn("可订票数");
        model.addColumn("出发时间");
        model.addColumn("到达时间");
        model.addColumn("所用时间");


        // 连接数据库并获取数据
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT air_airway.departure,air_airway.arrival," +
                    "air_flight.flight_id,air_flight.price,air_flight.available_seat,air_flight.departure_time,air_flight.arrival_time " +
                    "FROM air_airway,air_flight  " +
                    "WHERE air_airway.airway_id=air_flight.airway_id  " +
                    "ORDER BY air_flight.airway_id ASC ,air_flight.departure_time ASC");
//            select * from emp,dept where emp.dep_id=dept.id;
            while (resultSet.next()) {
                String departure = resultSet.getString("departure");
                String arrival = resultSet.getString("arrival");

                int flightId = resultSet.getInt("flight_id");
                String price = resultSet.getString("price");
                String availableSeat = resultSet.getString("available_seat");
                String departureTimeString = resultSet.getString("departure_time");
                String arrivalTimeString = resultSet.getString("arrival_time");


                //利用上面的departureTimeString和arrivalTimeString属性计算该航线需要的时间:timeDifference
                //解析时间格式
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                LocalDateTime departureTime = LocalDateTime.parse(departureTimeString, formatter);
                LocalDateTime arrivalTime = LocalDateTime.parse(arrivalTimeString, formatter);
                // 计算时间差,用于计算该航线到达目的地所需时间
                Duration duration1 = Duration.between(departureTime, arrivalTime);
                Duration duration = Duration.parse("" + duration1 + "");
                long hours = duration.toHours();
                long minutes = duration.toMinutes() / 60;
                String timeDifference = String.format("%d小时%d分钟", hours, minutes);

                Object[] rowData = {departure, arrival, flightId, price, availableSeat, departureTimeString, arrivalTimeString, timeDifference};
                model.addRow(rowData);
            }
            // 关闭连接
            resultSet.close();
            statement.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 创建表格并将模型设置到表格中
        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

        // 添加刷新按钮
        buttonAdd = new JButton("购票");
        buttonSearch = new JButton("搜索");

        buttonAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(() -> {
                    buyAirTicket buyAirTicketForm = new buyAirTicket();
                    buyAirTicketForm.setSize(380, 260);
                    buyAirTicketForm.setVisible(true);
                });
            }
        });

        buttonSearch.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(() -> {
                    searchBlock addFlightForm = new searchBlock();
                    addFlightForm.setSize(400, 300);
                    addFlightForm.setVisible(true);
                });
            }
        });

        JPanel Panel = new JPanel();
        Panel.setLayout(new FlowLayout());
        Panel.add(buttonAdd);
        Panel.add(buttonSearch);
        add(Panel, BorderLayout.SOUTH);
        JLabel jLabel = new JLabel("注意: 该表格是按照出发时间升序排列的,购票时需要填写航班id");
        jLabel.setSize(200, 200);
        Panel.add(jLabel);
    }

    public static void main(String[] args) {
//        SwingUtilities.invokeLater(() -> {
//            searchBlock showUserFlightInfoForm = new searchBlock();
//            showUserFlightInfoForm.setSize(1600, 300);
//            showUserFlightInfoForm.setVisible(true);
//        });
    }
}